/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VLSObjects;

import DBConnection.*;
import java.sql.*;

public class VehicleManager {
    
    public boolean checkVehicle(String manufacturer, String model, String vehicleID)
    {
        boolean result = false;

        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM vehiclelist WHERE manufacturer = ? AND model = ? AND vehicleID != ?");

            pstmt.setString(i++, manufacturer);
            pstmt.setString(i++, model);
            pstmt.setString(i++, vehicleID);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result = true;
            }

            conn.close();
        }
        catch(SQLException e)
        {
           e.printStackTrace();
        }

        return result;

    }
    
    public boolean addVehicle(Vehicle newVehicle)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;
            
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO vehiclelist ( manufacturer, model, size) VALUES(?,?,?)");
            //include parameters
            pstmt.setString(i++, newVehicle.getManufacturer());
            pstmt.setString(i++, newVehicle.getModel());
            pstmt.setString(i++, newVehicle.getSize());

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean editVehicle(Vehicle editVehicle)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("UPDATE vehiclelist SET manufacturer = ?, model = ?, size = ? WHERE vehicleID = ?");

            //include parameters
            pstmt.setString(i++, editVehicle.getManufacturer());
            pstmt.setString(i++, editVehicle.getModel());
            pstmt.setString(i++, editVehicle.getSize());
            pstmt.setString(i++, editVehicle.getVehicleID());
            

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean deleteVehicle(String vehicleID)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM vehiclelist WHERE vehicleID = ?");

            //include parameters
            pstmt.setString(i++, vehicleID);

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public String getVehicleID(String vehicleName)
    {   String vehicleID = "null";
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("SELECT vehicleID from vehiclelist WHERE model = ?");

            pstmt.setString(i++, vehicleName);
            
            //execute query
            ResultSet rs = pstmt.executeQuery();
            
            while(rs.next())
            {
            vehicleID = rs.getString(1);
            }

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return vehicleID;
    }
}
